<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="45%"
      v-dialogDrag
      append-to-body
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        v-loading="formLoading"
        label-width="100px"
      >
        <el-form-item label="图片地址" prop="img">
          <el-input v-model="formData.img" placeholder="请输入图片地址" />
          <img
            v-if="formData.img"
            :src="formData.img"
            alt="预览"
            class="preview-image"
          />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="formData.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="副标题" prop="subTitle">
          <el-input v-model="formData.subTitle" placeholder="请输入副标题" />
        </el-form-item>
        <el-form-item label="原价" prop="originalPrice">
          <el-input-number
            v-model="formData.originalPrice"
            controls-position="right"
            placeholder="请输入原价"
            :min="0"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="折扣后价格" prop="priceAfterDiscounts">
          <el-input-number
            v-model="formData.priceAfterDiscounts"
            controls-position="right"
            placeholder="请输入折扣后价格"
            :min="0"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">
          确 定
        </el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as ServiceInfothreeApi from "@/api/system/serviceinfothree";
export default {
  name: "ServiceInfothreeForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        img: undefined,
        title: undefined,
        subTitle: undefined,
        originalPrice: undefined,
        priceAfterDiscounts: undefined,
      },
      // 表单校验
      formRules: {
        img: [
          { required: true, message: "图片地址不能为空", trigger: "blur" },
          { type: "url", message: "请输入有效的URL地址", trigger: "blur" }
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" },
        ],
        subTitle: [
          { max: 100, message: "副标题长度不能超过100个字符", trigger: "blur" }
        ],
        originalPrice: [
          { required: true, message: "原价不能为空", trigger: "blur" },
        ],
        priceAfterDiscounts: [
          { required: true, message: "折扣后价格不能为空", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (value && this.formData.originalPrice && value > this.formData.originalPrice) {
                callback(new Error("折扣后价格不能高于原价"));
              } else {
                callback();
              }
            },
            trigger: "blur"
          }
        ],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await ServiceInfothreeApi.getServiceInfothree(id);
          this.formData = res.data;
          this.dialogTitle = "修改清洁服务";
        } finally {
          this.formLoading = false;
        }
      }
      this.dialogTitle = "新增清洁服务";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await ServiceInfothreeApi.updateServiceInfothree(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit("success");
          return;
        }
        // 添加的提交
        await ServiceInfothreeApi.createServiceInfothree(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit("success");
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        img: undefined,
        title: undefined,
        subTitle: undefined,
        originalPrice: undefined,
        priceAfterDiscounts: undefined,
      };
      this.resetForm("formRef");
    },
    resetForm(formRef) {
      this.$refs[formRef].resetFields();
    },
  },
};
</script>

<style scoped>
.preview-image {
  max-width: 100%;
  max-height: 200px;
  border: 1px solid #ccc;
  border-radius: 4px;
  margin-top: 10px;
}
</style>